package com.zmxstudy.gzu.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zmxstudy.gzu.domain.TbCategory;
import com.zmxstudy.gzu.mapper.TbCategoryMapper;
import com.zmxstudy.gzu.utils.ProductCodeMsg;
import com.zmxstudy.gzu.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.persistence.PostUpdate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@SuppressWarnings("rawtypes")
@Tag(name = "商品分类---三级分类")
@RestController
@RequestMapping(value = "/product-service/category")
public class CategoryController {

    @Resource
    TbCategoryMapper tbCategoryMapper;

    /*
     * 根据定义,添加PID=0为1级分类
     * */
    @Operation(summary = "查找一级分类")
    @GetMapping()
    @ResponseBody
    Result findTopCate() {
        LambdaQueryWrapper<TbCategory> laq = new LambdaQueryWrapper<>();
        laq.eq(TbCategory::getPid, 0);
        List<TbCategory> list = tbCategoryMapper.selectList(laq);
        return Result.success("成功", list);
    }

    @Operation(summary = "根据PID查询目录")
    @GetMapping("/{pid}")
    @ResponseBody
    Result findCateById(@PathVariable Long pid) {
        LambdaQueryWrapper<TbCategory> laq = new LambdaQueryWrapper<>();
        laq.eq(TbCategory::getPid, pid);
        List<TbCategory> list = tbCategoryMapper.selectList(laq);
        return Result.success("成功", list);
    }

    @Operation(summary = "根据PID添加目录")
    @PostMapping("/{pid}")
    @ResponseBody
    public Result addCateById(@PathVariable Long pid, @RequestBody TbCategory tbCategory) {
        System.out.println(tbCategory);
        System.out.println(pid);
        if (pid != 0){
            TbCategory tbCategory1 = tbCategoryMapper.selectById(pid);
            if (tbCategory1 == null) {
                return Result.error(ProductCodeMsg.ADD_error);
            }
        }
        tbCategory.setPid(pid);
        System.out.println(tbCategory);
        tbCategoryMapper.insert(tbCategory);
        return Result.success();
    }

    @Operation(summary = "修改目录")
    @PutMapping()
    @ResponseBody
    public Result updateCateById(@RequestBody TbCategory tbCategory) {
        LambdaQueryWrapper<TbCategory> laq = new LambdaQueryWrapper<>();
        laq.eq(TbCategory::getPid, tbCategory.getPid());
        List<TbCategory> list = tbCategoryMapper.selectList(laq);
        int judge = 0;
        for (TbCategory category: list) {
            if (tbCategory.getCategoryName().equals(category.getCategoryName())){
                judge = judge + 1;
            }
        }
        if (judge > 0 ){
            return Result.error(ProductCodeMsg.UPADTE_error_TbCategory);
        }
        tbCategoryMapper.updateById(tbCategory);
        return Result.success();
    }

    @Operation(summary = "删除目录")
    @DeleteMapping("/{id}")
    @ResponseBody
    public Result deleteCateById(@PathVariable Long id) {
        TbCategory tbCategory = tbCategoryMapper.selectById(id);

        if (tbCategory == null){
            return Result.error(ProductCodeMsg.DELETE_error_TbCategory1);
        }

        LambdaQueryWrapper<TbCategory> laq = new LambdaQueryWrapper<>();
        laq.eq(TbCategory::getPid, id);
        List<TbCategory> list = tbCategoryMapper.selectList(laq);


        if (list.size() > 0 ){
            return Result.error(ProductCodeMsg.DELETE_error_TbCategory);
        }

        tbCategoryMapper.deleteById(id);
        return Result.success();
    }
}
